/* -----------------------------------------------------------------------
 * Paul Cohen
 * CS 362, Summer 2014
 * Assignment 3
 *
 * unittest2.c - testing Dominion's drawCard() function
 * 
 * -----------------------------------------------------------------------
 */

#include "unittest2.h"
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>

#define NUM_TESTS 100

int testDrawCard(int numPlayers, struct gameState *g)
{
    int beforeHandCount = 0;
    int afterHandCount = 0;
    
    // pick random player
    int player = Random() * numPlayers;

    // store current hand count
    beforeHandCount = g->handCount[player];

    // set deckCount to 1
    g->deckCount[player] = 1;
    
    drawCard(player, g);
    afterHandCount = g->handCount[player];
    
    // make sure hand count is incremented and deck count is decremented
    if (afterHandCount != (beforeHandCount + 1))
        return 0;
    if (g->deckCount[player] != 0) 
        return 0;

    // check drawCard behavior when deck is empty
    drawCard(player, g);
    if(g->deckCount[player] < 0)
        return 0;

    return 1;
}

int unit_test2()
{
    struct gameState g;
    int i, j, seed, successes = 0, totalTests = 0, testResult;
    int k[10] = {adventurer, gardens, great_hall, village, minion, mine, cutpurse, baron, salvager, smithy};
    printf("\nUnit Test 2: testing drawCard()\n------------\n");
    for (i = 0; i < NUM_TESTS; i++)
    {
        for (j = 2; j <= MAX_PLAYERS; j++)
        {
            seed = 1000 + i;
            memset(&g, 0, sizeof(struct gameState));   // clear the game state
            initializeGame(j, k, seed, &g);
            testResult = testDrawCard(j, &g);
            if (testResult)
                successes++;
            totalTests++;
        }
    }

    printf("drawCard() tests completed!\n");
    printf("%d out of %d tests passed.\n", successes, totalTests);
    
    return 0;
}